package com.example.infrastructure.infrastructure.mapper.person;

import com.example.infrastructure.api.organization.dto.OrganizationDto;
import com.example.infrastructure.api.person.dto.CompoundPersonDto;
import com.example.infrastructure.api.person.dto.PersonDto;
import com.example.infrastructure.api.person.vo.PersonVo;
import com.example.infrastructure.application.OrganizationApplication;
import com.example.infrastructure.domain.person.aggregate.Person;
import com.example.infrastructure.domain.person.repo.po.PersonPo;
import com.example.infrastructure.infrastructure.mapper.CycleAvoidingMappingContext;
import com.example.infrastructure.infrastructure.mapper.organization.OrganizationMapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * TODO
 *
 * @author lxh
 * @date 2020/11/23 上午11:50
 **/
@Mapper(componentModel = "spring")
public abstract class PersonMapper {
    @Autowired
    protected OrganizationApplication organizationApplication;

    public abstract List<PersonDto> toDtoList(List<Person> people);

    public abstract List<CompoundPersonDto> toCompoundPersonDtoList(List<Person> people);

    @Mapping(source = "orgId", target = "organization")
    public abstract CompoundPersonDto toCompoundPersonDto(Person person);

    public abstract PersonDto toDto(Person person);

    public abstract Person toDo(PersonPo po);

    public abstract Person toDo(PersonVo vo);

    public abstract PersonPo toPo(Person person);

    public OrganizationDto orgIdToOrganizationDto(String orgId) {
        return organizationApplication
                .findOne(orgId)
                .map(organization -> OrganizationMapper.INSTANCE.toDto(organization, new CycleAvoidingMappingContext()))
                .orElse(null);
    }
}
